# client.py
import os
import shutil
import time
from datetime import datetime
import requests
import zipfile

# SERVER_URL = 'http://zhida.sheyueai.com/mineru2/parse'
SERVER_URL = 'http://zhida.sheyueai.com/mineru/parse'
# SERVER_URL = 'http://118.89.134.53/mineru/parse'


# SERVER_URL = 'http://localhost:5000/parse'

def upload_pdf(filepath):
    try:
        print(f"📄 正在上传：{filepath}")
        filepath = filepath.replace('\\', '/')
        with open(filepath, 'rb') as f:
            files = {'pdf': (os.path.basename(filepath), f)}
            headers = {
                # 'Authorization': 'Bearer your_token_here',  # 如果需要认证
                # 'Content-Type': 'multipart/form-data',     # requests 会自动设置
            }
            response = requests.post(SERVER_URL, files=files, headers=headers,stream=True, timeout=6000,verify=False)
            if response.status_code == 200:
                output_zip = os.path.splitext(filepath)[0] + '_result.zip'
                with open(output_zip, 'wb') as out_file:
                    for chunk in response.iter_content(chunk_size=1024):
                        if chunk:
                            out_file.write(chunk)
                print(f"✅ 解析完成并保存至 {output_zip}")
                # 解压 ZIP 到原文件目录
                extract_dir = os.path.dirname(filepath)
                unzip_and_clean(output_zip, extract_dir)
            else:
                print(f"❌ 请求失败：{response.text}")
                # if(response.text.find('An error occurred during processing') != -1):
                #     #     删除文件
                #     os.remove(filepath)

                # time.sleep(3)
            response.close()
            # time.sleep(5)
    except Exception as e:
        print(f"❌ 上传失败：{e}")


def unzip_and_clean(zip_path, extract_to):
    try:
        with zipfile.ZipFile(zip_path, 'r') as zip_ref:
            zip_ref.extractall(extract_to)
        print(f"📦 已解压到：{extract_to}")

        # 删除 ZIP 文件
        os.remove(zip_path)
        print(f"🗑️ 已删除压缩包：{zip_path}")
    except Exception as e:
        print(f"❌ 解压失败：{e}")


def walk_and_upload(folder):
    # 计算数量
    total_files = []
    for root, _, files in os.walk(folder):

        for file in files:
            if file.lower().endswith('.pdf'):
                if (file.lower().endswith('_model.pdf') or file.lower().endswith('_layout.pdf')
                        or file.lower().endswith('_spans.pdf')
                        or file.lower().endswith('_origin.pdf')):
                    continue
                filepath = os.path.join(root, file)
                name_without_ext = os.path.splitext(file)[0]
                md_file = os.path.join(root, f"{name_without_ext}.md")

                # 如果对应的 .md 文件存在，跳过
                if os.path.exists(md_file):
                    print(f"📎 跳过 {filepath}，因为存在同名的 {md_file}")
                    continue
                jrf = 'C:\\Users\\admin\\Documents\\金融文献库'
                # filepath 去掉 folder拼接 jrf
                print(root)
                jrff = root.replace(folder, jrf)
                print(jrff)
                print(os.path.join(jrff, f"{name_without_ext}.md"))
                # 如果 文件在 金融文献库 中，跳过
                if os.path.exists(os.path.join(jrff, f"{name_without_ext}.md")):
                    print(f"📎 跳过 {filepath}，因为已存在 {os.path.join(jrf, file)}")
                    # 复制文件到当前文件
                    try:
                        shutil.copy(os.path.join(jrff, f"{name_without_ext}.md"), os.path.join(root, f"{name_without_ext}.md"))
                        shutil.copy(os.path.join(jrff, f"{name_without_ext}_model.pdf"), os.path.join(root, f"{name_without_ext}_model.pdf"))
                        shutil.copy(os.path.join(jrff, f"{name_without_ext}_layout.pdf"), os.path.join(root, f"{name_without_ext}_layout.pdf"))
                        shutil.copy(os.path.join(jrff, f"{name_without_ext}_spans.pdf"), os.path.join(root, f"{name_without_ext}_spans.pdf"))
                        shutil.copy(os.path.join(jrff, f"{name_without_ext}_content_list.json"), os.path.join(root, f"{name_without_ext}_content_list.json"))
                        shutil.copytree(os.path.join(jrff, "images"),  os.path.join(root, f"images"))
                    except Exception as e:
                        print(f"❌ 复制失败：{e}")
                    continue
                # print(f"📄 正在上传：{filepath}")
                # total_files.append(filepath)
                total_files.insert(0,filepath)
    print("开始上传",len(total_files))
    i = 1
    for total_file in total_files:
        # 输出 时间-进度

        formatted_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print(f"{formatted_time}\t\t{i}/{len(total_files)} ")
        print(total_file)
        i += 1

        upload_pdf(total_file)

if __name__ == '__main__':
    folder_path = input("请输入要扫描的PDF文件夹路径：").strip()
    walk_and_upload(folder_path)
